using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing.Design;

namespace RPGOnline.Controls.Map.Layers.Lighting
{
    [TypeConverter(typeof(ExpandableObjectConverter))]
    [Editor(typeof(LightsCollectionEditor), typeof(UITypeEditor))]
    public class LightsCollection : CollectionBase, ICustomTypeDescriptor
    {
        private readonly Map m_map;
        private readonly LightingLayer m_lightingLayer;

        public LightsCollection(Map map)
        {
            m_map = map;
        }

        public void Add(Light light)
        {
            List.Add(light);
        }

        protected override void OnInsert(int index, object value)
        {
            Light light = value as Light;

            light.Map = m_map;

            base.OnInsert(index, value);
        }

        public AttributeCollection GetAttributes()
        {
            return TypeDescriptor.GetAttributes(typeof(LightsCollection));
        }

        public string GetClassName()
        {
            return TypeDescriptor.GetClassName(typeof(LightsCollection));
        }

        public string GetComponentName()
        {
            return TypeDescriptor.GetComponentName(typeof(LightsCollection));
        }

        public TypeConverter GetConverter()
        {
            return TypeDescriptor.GetConverter(typeof(LightsCollection));
        }

        public EventDescriptor GetDefaultEvent()
        {
            return TypeDescriptor.GetDefaultEvent(typeof(LightsCollection));
        }

        public PropertyDescriptor GetDefaultProperty()
        {
            return TypeDescriptor.GetDefaultProperty(typeof(LightsCollection));
        }

        public object GetEditor(Type editorBaseType)
        {
            return TypeDescriptor.GetEditor(typeof(LightsCollection), editorBaseType);
        }

        public EventDescriptorCollection GetEvents()
        {
            return TypeDescriptor.GetEvents(typeof(LightsCollection));
        }

        public EventDescriptorCollection GetEvents(Attribute[] attributes)
        {
            return TypeDescriptor.GetEvents(typeof(LightsCollection), attributes);
        }

        public PropertyDescriptorCollection GetProperties()
        {
            return TypeDescriptor.GetProperties(typeof(LightsCollection));
        }

        public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
        {
            List<PropertyDescriptor> properties = new List<PropertyDescriptor>();
            int index = 0;
            foreach(Light light in this)
            {
                properties.Add(new LightPropertyDescriptor(index++, light));
            }

            return new PropertyDescriptorCollection(properties.ToArray());
        }

        public object GetPropertyOwner(PropertyDescriptor pd)
        {
            return this;
        }

        [TypeConverter(typeof(ExpandableObjectConverter))]
        class LightPropertyDescriptor : PropertyDescriptor
        {
            private Light m_light;

            public LightPropertyDescriptor(int index, Light light) : base("Light " + index, new Attribute[] {})
            {
                m_light = light;
            }

            public override bool CanResetValue(object component)
            {
                return false;
            }

            public override object GetValue(object component)
            {
                return m_light;
            }

            public override void ResetValue(object component)
            {
            }

            public override void SetValue(object component, object value)
            {
                m_light = value as Light;
            }

            public override bool ShouldSerializeValue(object component)
            {
                return false;
            }

            public override Type ComponentType
            {
                get { return typeof (Light); }
            }

            public override bool IsReadOnly
            {
                get { return false; }
            }

            public override Type PropertyType
            {
                get { return typeof (Light); }
            }
        }
    }
}